package com.example.demo;

/**
 * 比较版本号，从左侧开始比较，后面缺失的为0
 * https://leetcode.cn/problems/compare-version-numbers/
 */
public class CompareVersion {

    public int compareVersion(String version1, String version2) {
        String[] arr1 = version1.split("\\.");
        String[] arr2 = version2.split("\\.");
        int length1 = arr1.length, length2 = arr2.length;
        int length = Math.max(length1, length2);

        for (int i = 0; i < length; i++) {
            int revision1 = i < length1 ? Integer.parseInt(arr1[i]) : 0;
            int revision2 = i < length2 ? Integer.parseInt(arr2[i]) : 0;
            if (revision1 != revision2) {
                return revision1 < revision2 ? -1 : 1;
            }
        }

        return 0;
    }
}
